package com.ja.openchat.ui.found;

import android.content.Context;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.google.gson.JsonObject;
import com.ja.openchat.R;
import com.ja.openchat.adapters.CircleHomeAdapter;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.circle.BlogInfo;
import com.zy.openchat.core.model.circle.BlogReplyInfo;
import com.zy.openchat.core.model.circle.BlogReplysInfo;
import com.zy.openchat.core.model.circle.InputBlogIdType;
import com.zy.openchat.core.model.circle.OnReplyScollEvent;
import com.zy.openchat.core.model.circle.RefreshBlogEvent;
import com.zy.openchat.core.model.circle.RefreshBlogReplyEvent;
import com.zy.openchat.util.Common;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;

public class CircleCommentDialog extends BottomPopupView {

    private RecyclerView recyclerView;
    private TextView top_title;
    private CircleHomeAdapter mAdapter;
    private List<MultiItemEntity> mData = new ArrayList<>();
    private BaseViewModel mViewModel;
    private BlogInfo mBlogInfo;
    private OnCircleCommentListener onCircleCommentListener;
    public CircleCommentDialog(@NonNull Context context, BaseViewModel viewModel, BlogInfo blogInfo, OnCircleCommentListener onCircleCommentListener) {
        super(context);
        mViewModel = viewModel;
        mBlogInfo = blogInfo;
        this.onCircleCommentListener = onCircleCommentListener;
    }


    @Override
    protected void onCreate() {
        super.onCreate();
        EventBus.getDefault().register(this);
        if(mBlogInfo==null){
            dismiss();
            return;
        }
        top_title = findViewById(R.id.top_title);
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new CircleHomeAdapter(3,mData,mViewModel);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setEmptyView(R.layout.status_center_empty_view_wrap);
        mAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                currentPage++;
                requestReplyInfo(currentPage);
            }
        });
        findViewById(R.id.tv_temp).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showRemarkBar(new InputBlogIdType(true,mBlogInfo.id),getContext().getString(R.string.comment_text),v);
            }
        });
        findViewById(R.id.close_btn).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if(view.getId() !=  R.id.comment_btn){
                    mAdapter.onItemChildClick(adapter,view,position);
                }
                MultiItemEntity multiItemEntity = mAdapter.getItem(position);
                if(multiItemEntity==null){
                    return;
                }
                switch (view.getId()){
                    case R.id.remark_et:
                    case R.id.comment_btn:
                        showRemarkBar(new InputBlogIdType(true,mBlogInfo.id),getContext().getString(R.string.comment_text),view);
                        break;
                    case R.id.remark_btn:
                        if(multiItemEntity instanceof BlogReplyInfo){
                            showRemarkBar(new InputBlogIdType(false,((BlogReplyInfo)multiItemEntity).id),getContext().getString(R.string.comment_text),(View) view.getParent());
                        }
                        break;
                }
            }
        });
        top_title.setText(String.format(getContext().getString(R.string.comment_number_text),mBlogInfo.replyNumber()));
        requestReplyInfo(0);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTgEvent(final RefreshBlogReplyEvent tg_event) {
        currentPage = 0;
        requestReplyInfo(currentPage);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTgEvent(final RefreshBlogEvent tg_event) {
        top_title.setText(String.format(getContext().getString(R.string.comment_number_text),mBlogInfo.replyNumber()));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTgEvent(final OnReplyScollEvent tg_event) {
        if(tg_event.inputViewCoordinatesOnScreen!=null){
            recyclerView.smoothScrollBy(0,tg_event.inputViewCoordinatesOnScreen[1],new LinearInterpolator());
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void showRemarkBar(InputBlogIdType blogIdType, String hint, View view){
        if(onCircleCommentListener!=null){
            onCircleCommentListener.onCircleComment(blogIdType,hint,view);
        }
    }

    private int currentPage = 0;
    private void requestReplyInfo(int page){
        TelegramManager.getTelegramManager().getBlogReplys(getContext(), mBlogInfo.id, 20, page*20, new TgResultBlock<BlogReplysInfo>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, BlogReplysInfo obj) {
                if(page == 0){
                    mData.clear();
                }
                if(obj==null|| Common.isEmpty(obj.replys)){
                    mAdapter.notifyDataSetChanged();
                    mAdapter.getLoadMoreModule().loadMoreEnd();
                }else{
                    mData.addAll(obj.replys);
                    mAdapter.notifyDataSetChanged();
                    if(obj.replys.size()<20){
                        mAdapter.getLoadMoreModule().loadMoreEnd();
                    }else{
                        mAdapter.getLoadMoreModule().loadMoreComplete();
                    }

                }
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                mAdapter.getLoadMoreModule().loadMoreEnd();
            }
        });
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_circle_comment;
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getAppHeight(getContext())*.7f);
    }

    @Override
    public int getPopupHeight() {
        return (int) (XPopupUtils.getAppHeight(getContext())*.7f);
    }

}
